var RepairCountInfo = function (repairList, year) {
  BaseModel.call(this);
  var self = this;

  var fn = {
    initData: function (repairList, year) {
      self.year = year;
      if (repairList.length === 0) {
        self.maxCount = 0;
        self.nameList = [];
        return;
      }
      var obj = _.maxBy(repairList, 'applyCount');
      self.maxObj = obj;
      self.maxCount = obj.applyCount;

      var repairGroup = _.groupBy(repairList, 'work_name');
      var nameList = _.keys(repairGroup);
      nameList = nameList.sort(function (value1, value2) {
        return value1.localeCompare(value2);
      });
      self.nameList = nameList;
      self.repairGroup = repairGroup;
      this.setRepairData(repairGroup);
    },

    setRepairData: function (repairGroup) {
      var nameList = self.nameList;
      var year = self.year;

      var month = 12;
      if (moment(year).isSame(moment(), 'year')) {
        month = moment().get('month') + 1;
      }

      var repairList = [];
      for (var i = 0; i < nameList.length; i++) {
        var name = nameList[i];
        var repairs = repairGroup[name];
        var monthRepairData = this.getMonthRepairData(repairs, month);
        repairList.push({
          name: name,
          data: monthRepairData
        });
      }
      self.repairList = repairList;
    },

    getMonthRepairData: function (repairs, month) {
      var repairList = [];
      for (var i = 1; i <= month; i++) {
        repairList.push({
          month: i,
          applyCount: 0
        });
      }

      for (var i = 0; i < repairs.length; i++) {
        var repair = repairs[i];
        var month = repair.monthTime;
        var index = month - 1;
        var obj = repairList[index];
        if (obj === undefined) {
          repairList.push({
            month: i,
            applyCount: repair.applyCount
          });
        } else {
          obj.applyCount = repair.applyCount;
        }
      }
      repairList = _.orderBy(repairList, ['month'], ['asc']);
      return repairList;
    }
  };

  this.getChartData = function () {
    var repairList = this.repairList;
    var headerData = ['name'];
    headerData.pushArray(_.map(repairList[0].data, 'month'));
    var chartData = [headerData];
    for (var i = 0; i < repairList.length; i++) {
      var repair = repairList[i];
      var dataList = [repair.name];
      var data = repair.data;
      data = _.map(data, 'applyCount');
      dataList.pushArray(data);
      chartData.push(dataList);
    }

    return chartData;
  };

  this.getMoveCoords = function () {
    var repairList = this.repairList;
    var name = this.maxObj.work_name;
    var repair = _.find(repairList, {
      name: name
    });
    var data = _.map(repair.data, function (obj) {
      return [obj.month - 1, obj.applyCount];
    });
    return data;
  };

  fn.initData(repairList, year);
};
